package com.cloud.demo.models.bus.controller;

import com.cloud.demo.common.enums.ResultEnum;
import com.cloud.demo.common.web.domain.respomse.Result;
import com.cloud.demo.models.bus.domian.BusOrderDetailed;
import com.cloud.demo.models.bus.qo.OrderQo;
import com.cloud.demo.models.bus.service.BusOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/bus/order")
@Tag(name = "商业订单接口")
public class BusOrderController {

    @Autowired
    private BusOrderService busOrderService;

    @PostMapping("selectOrder")
    @Operation(summary = "订单查询",description = "通过时间或客户名或订单号范围查询")
    public Result selectOrder( @RequestBody OrderQo qo){

        PageInfo<BusOrderDetailed> busOrderPage = busOrderService.pageList(qo);
        return Result.build(ResultEnum.SUCCESS,busOrderPage);
    }


    @PostMapping("inset")
    @Operation(summary = "插入订单",description = "插入包括订单明细以及订单下面多个产品")
    public Result insetOrder(@RequestBody BusOrderDetailed busOrderDetailed,
                             @RequestParam String orderNum){
        //查看订单号是否已经存在，如果存在，更新订单号;然后再插入
        busOrderService.insetOrder(busOrderDetailed,orderNum);

        return Result.build(ResultEnum.SUCCESS,null);
    }
    @PostMapping("update")
    @Operation(summary = "修改订单",description = "修改订单明细以及订单下面多个产品")
    public Result update(@RequestBody BusOrderDetailed busOrderDetailed){
        //查看订单号，更新订单号;然后再插入
        busOrderService.updateOrder(busOrderDetailed);

        return Result.build(ResultEnum.SUCCESS,null);
    }
    @GetMapping("orderNum")
    public Result orderNum(){
        String orderNum = busOrderService.orderNum();
        return Result.build(ResultEnum.SUCCESS,orderNum);
    }
}
